import { WeaSearchGroup, WeaFormItem, WeaTools } from 'ecCom';
import { Button, Row, Col } from 'antd';
import { toJS } from 'mobx';
import { WeaSwitch } from 'comsMobx';
import { inject, observer } from 'mobx-react';

const getKey = WeaTools.getKey;
import MonthDayGroup from '../comp/month-day-group/MonthDayGroup';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
@observer
class Condition extends React.Component {
  constructor() {
    super();
  }

  shouldComponentUpdate() {
    return true;
  }

  render() {
    // const { condition} = this.props.listStore;
    const { listStore, form, onEnterSearch } = this.props;
    const { condition } = listStore;
    const { isFormInit } = form;
    const group = [];
    this.formParams = form.getFormParams();
    isFormInit && condition.map((c, indexGroup) => {
      const items = [];
      c.items.map((fields, index) => {
        let dom;

        if (fields.conditionType == 'MONTH_DAY') { // 客户生日-高级搜索
          const key = getKey(fields);
          const bindObj = form.$(key);
          dom = <MonthDayGroup ecId={`${this && this.props && this.props.ecId || ''}_MonthDayGroup@8l0z1a@${index}`} {...bindObj.bind()} value={[form.getFormParams()[fields.domkey[0]], form.getFormParams()[fields.domkey[1]]]} />;
        } else {
          dom = <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@wfvd8t@${index}`} fieldConfig={fields} form={form} formParams={form.getFormParams()} onEnterSearch={onEnterSearch} />;
        }

        items.push({
          com: (<WeaFormItem
            ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@wuu51a@${index}`}
            label={`${fields.label}`}
            labelCol={{ span: `${fields.labelcol}` }}
            wrapperCol={{ span: `${fields.fieldcol}` }}
          >
            {dom}
          </WeaFormItem>),
          colSpan: 1,
        });
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@bigwlu@${indexGroup}`} needTigger title={c.title} showGroup={c.defaultshow} items={items} />);
    });
    return group;
  }
}

const getAdButtons = (listStore, ecId) => {
  const { doSearch, setShowSearchAd, clearFormFields } = listStore;
  return [
    (<Button ecId={`${ecId}_Button@287w6x@search`} type="primary" onClick={() => { doSearch(); setShowSearchAd(false); }}>{getLabel(197, '搜索')}</Button>),
    (<Button ecId={`${ecId}_Button@p0wj32@reset`} type="ghost" onClick={() => { clearFormFields(); }}>{getLabel(2022, '重置')}</Button>),
    (<Button ecId={`${ecId}_Button@ol0wh8@cancel`} type="ghost" onClick={() => { setShowSearchAd(false); }}>{getLabel(201, '取消')}</Button>),
  ];
};

export { Condition, getAdButtons };
